import tkinter as tk
from tkinter import messagebox
from words import Word_Manager
from key_display import Key_Dis

L_X = 50
L_H = 20
B_H = 3
B_W = 8
B_P = 690

class Wordle_UI:
    def __init__(self, root: tk.Tk,this_wm:Word_Manager,this_kd:Key_Dis):
        # 杂乱的参数部分
        self.letters: list[tk.Label] = []
        self.root = root
        self.heart = 5

        # 重要部分
        self.wm = this_wm
        self.kd = this_kd

        # 初始化字母以及entry,button等
        self.init_widget()
        self.init_letters()

    # 前置重启函数
    def pre_again(self):
        for i in range(5):
            self.letters[i].config(text=" ", bg=["green", "lime"][i % 2])
        self.entry.delete(0, 100)

        # 更新剩余次数
        self.times.config(text=f"剩余提交次数:{self.heart}")
        self.heart = 8
        # 更新单词
        self.wm.word = self.wm.choose_word()
        # 测试用
        print(self.wm.word)

    # 三个按钮的函数
    def again(self):
        if messagebox.askyesno("重新开始","确定新来一局?"):
            self.pre_again()

    def quit(self):
        if messagebox.askyesno("退出游戏","是否退出游戏？？？"):
            self.root.quit()

    def submit(self):
        input_word = self.entry.get().strip().lower()
        self.main_logic(input_word)
        self.times.config(text=f"剩余提交次数:{self.heart}")

    # 游戏已经结束了
    def after_lose(self):
        if messagebox.askyesno("游戏结束",
        f"恭喜您失败,本居单词为{self.wm.word},不服再来一局?"):
            self.pre_again()
        else:
            self.root.quit()

    def after_win(self):
        # for i in range(5):
        #     self.letters[i].config(text=self.wm.word[i], bg=["green", "lime"][i % 2])
        if messagebox.askyesno("游戏结束",
        f"恭喜您超过全国{self.wm.win_chance()}%的玩家,是否再来一局?"):
            self.pre_again()
        else:
            self.root.quit()

    # 初始化组件部分
    def init_letters(self):
        # for循环遍历添加
        for i in range(5):
            letter = tk.Label(self.root,text = " ",width=4,height=2,bg=["green","lime"][i%2]
                              ,font=("fantasy",25),)
            letter.place(x = i*80+L_X,y=L_H)
            self.letters.append(letter)

    def init_widget(self):
        self.entry:tk.Entry = tk.Entry(self.root,
                              width=30,
                              bg="beige",
                              fg="magenta",
                              font=("Arial",20))
        self.quit_ = tk.Button(self.root,text="退出游戏",bg="pink",font=("serif",16),
                              command=self.quit,height=B_H,width=B_W)
        self.again_ = tk.Button(self.root,text="重新开始",bg="pink",font=("serif",16)
                               ,command=self.again,height=B_H,width=B_W)
        self.submit_ = tk.Button(self.root,text="提交单词",bg="pink",font=("serif",16)
                               ,command=self.submit,height=B_H,width=B_W)
        self.times = tk.Label(self.root,text="剩余提交次数:{}".format(self.heart),
                              fg="purple",font=("Arial",20),width="20",height=1)

        self.entry.place(x = 270,y = 640)
        self.quit_.place(x = 430,y = B_P)
        self.submit_.place(x = 730,y = B_P)
        self.again_.place(x = 130,y = B_P)
        self.times.place(x = 550,y = 300)

    # 更新字母部分
    def update_letters(self,processed_word:dict):
        for i in range(5):
            match processed_word[i][1]:
                case 0:
                    self.letters[i].config(text=processed_word[i][0],
                                           bg=["green","lime"][i%2])
                case 1:
                    self.letters[i].config(text=processed_word[i][0],
                                           bg="gold")
                case 2:
                    self.letters[i].config(text=processed_word[i][0],
                                          bg="red")

    # 警告输入错误
    def entry_jump(self,text:str):
        self.jalmot = tk.Label(self.root,text=text,fg="red",
                               width=20,height=1,font=("Arial",14))
        self.jalmot.place(x=375,y=600)

    # 主要逻辑部分
    def main_logic(self,input_word):
        # 处理单词
        process_word = self.wm.check_right(input_word,)
        if process_word == 0:
            self.entry_jump("请输入正确的单词！！！")
        elif process_word == -1:
            self.entry_jump("请不要不输入单词！！！")
        else:
            try:
                self.jalmot.config(text="")
            except:
                pass
            self.heart -= 1

        # 应该立刻更新字母
        if process_word != 0 and process_word != -1:
            self.update_letters(process_word)
        # 输了
        if self.heart == 0:
            self.after_lose()
        # 赢了
        if self.wm.word == input_word:
            self.after_win()
            # 但是还是得更新


